﻿using Ingr.SP3D.Common.Client;
using Ingr.SP3D.Common.Client.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using IMSTrader;

namespace SWS.PIP.CMD
{
    public class PipeSpoolCheckCmd: BaseModalCommand
	{
		PipeSpoolManangeForm m_Form;
		private Trader m_Trader;

		public override void OnStart(int commandID, object argument)
		{
			try
			{
				if (this.m_Trader == null)
				{
					this.m_Trader = new TraderClass();
				}
				this.ShowCheckForm();
			}
			catch (Exception exception)
			{
				MessageBox.Show("启动命令失败，失败原因：\n" + exception.Message);
				//base.DebugLogMessage("PipeSpoolCheckCmd::OnStart:" + exception.Message);
			}
		}

		internal void ShowCheckForm()
		{
			if (ClientServiceProvider.GraphicViewMgr.ActiveView != null)
			{
				if (this.m_Form != null)
				{
					this.m_Form.BringToFront();
					this.m_Form.Visible = true;
					if (this.m_Form.WindowState == FormWindowState.Minimized)
					{
						this.m_Form.WindowState = FormWindowState.Normal;
					}
				}
				else
				{
					object target = this.m_Trader.get_Service("MainFrame", "");
					int num = Convert.ToInt32(target.GetType().InvokeMember("hWnd", BindingFlags.GetProperty, null, target, null));
					target = null;
					this.m_Form = new PipeSpoolManangeForm();
					this.m_Form.FormClosing += new FormClosingEventHandler(this.MDRResultsForm_FormClosing);
					this.m_Form.Show(new CWindowWrapper(new IntPtr(num)));
				}
			}
		}

		internal void MDRResultsForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (this.m_Form != null)
			{
				this.m_Form.FormClosing -= new FormClosingEventHandler(this.MDRResultsForm_FormClosing);
				this.m_Form = null;
			}
		}

		private class CWindowWrapper : IWin32Window
		{
			private IntPtr _hwnd;

			public CWindowWrapper(IntPtr handle)
			{
				this._hwnd = handle;
			}

			public IntPtr Handle
			{
				get
				{
					return this._hwnd;
				}
			}
		}
	}
}
